import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Random;

import sort.ISort;
import sort.Insertionsort;
import sort.Mergesort;
import sort.Quicksort;

public class Apl {
	private NumberFormat formatter;
	
	public Apl() {
		formatter = new DecimalFormat("#0.00000");
		
		// Standard sort
		standardsort();
		quicksort();
		insertionsort();
		mergesort();
	}
	
	private void standardsort() {
		int[] elements = generateNumbers(1000, 1000);
		
		long startTime = System.currentTimeMillis();
	    Arrays.sort(elements);
	    long endTime = System.currentTimeMillis();
	    System.out.println("Standard Java sort " + formatter.format((endTime - startTime) / 1000d) +" seconds.");
	}
	
	private void quicksort() {
		int[] elements = generateNumbers(1000, 1000);
		
		long startTime = System.currentTimeMillis();
		ISort sort = new Quicksort();
		sort.doSort(elements);
	    long endTime = System.currentTimeMillis();
	    System.out.println("Quicksort " + formatter.format((endTime - startTime) / 1000d) +" seconds.");
	}
	
	private void insertionsort() {
		int[] elements = generateNumbers(1000, 1000);
		
		long startTime = System.currentTimeMillis();
		ISort sort = new Insertionsort();
		sort.doSort(elements);
	    long endTime = System.currentTimeMillis();
	    System.out.println("Insertionsort " + formatter.format((endTime - startTime) / 1000d) +" seconds.");
	}
	
	private void mergesort() {
		int[] elements = generateNumbers(0100, 1000);
		
		long startTime = System.currentTimeMillis();
		ISort sort = new Mergesort();
		sort.doSort(elements);
	    long endTime = System.currentTimeMillis();
	    System.out.println("Mergesort " + formatter.format((endTime - startTime) / 1000d) +" seconds.");
	}

	private int[] generateNumbers(int totalNumbers, int maxValue) {
		int[] output = new int[totalNumbers];

		Random random = new Random();
		for (int i = totalNumbers - 1; i >= 0; i--) {
			output[i] = random.nextInt(maxValue) + 1;
		}
		
		return output;
	}
	
	public static void main(String[] args) {
		new Apl();
	}
}
